﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using monitor_2_DBwork;

namespace monitor_server_2_WPF
{
    partial class ControlTabControl
    {

        private const string LogsDelayOptName = "SaveDeviceLogsDelay";
        private const string BizssControlEnabledOptName = "BizssControlEnabled";
        private const string BizssControlDelayOptName = "BizssControlDelay";


        void InitMonitoringTab()
        {
            GeneralGroupLabel.Content = Res.Rm.GetString("GeneralGroupLabel");
            LogsTimeoutLabel.Content = Res.Rm.GetString("LogsTimeoutLabel");

            BizssGroupLabel.Content = Res.Rm.GetString("BizssGroupLabel");
            BizssStateLabel.Content = Res.Rm.GetString("BizssStateLabel");
            BizssTimeoutLabel.Content = Res.Rm.GetString("BizssTimeoutLabel");
        }

        private void MonitoringItem_OnLoaded(object sender, RoutedEventArgs e)
        {
            OptionsReset();
            SetStatus(OptionsStatus.Saved);
        }
        //BizssControlDelay:2;              -- Bizss delay
        //BizssControlEnabled:true;         -- Bizss enabled
        //SaveDeviceLogsDelay:2;            -- Logs delay
        
        void OptionsReset()
        {
            var lDelay = DBwork.GetOptionValue(LogsDelayOptName);  //Need restart
            int lDelayInt;
            if (int.TryParse(lDelay, out lDelayInt))
            {
                LogsTimeoutBox.Value = lDelayInt;
            }
            else
            {
                LogsTimeoutBox.Content = "N/A";
                LogsTimeoutBox.IsEnabled = false;
            }

            var bState = DBwork.GetOptionValue(BizssControlEnabledOptName);  //Need restart
            bool bStateBool;
            if (bool.TryParse(bState, out bStateBool))
            {
                BizssStateBox.IsChecked = bStateBool;
            }
            else
            {
                BizssStateBox.Content = "N/A";
                BizssStateBox.IsEnabled = false;
            }

            var bDelay = DBwork.GetOptionValue(BizssControlDelayOptName);    //Need restart
            int bDelayInt;
            if (int.TryParse(bDelay, out bDelayInt))
            {
                BizssTimeoutBox.Value = bDelayInt;
            }
            else
            {
                BizssTimeoutBox.Content = "N/A";
                BizssTimeoutBox.IsEnabled = false;
            }
        }

        private void BizssStateBox_OnChecked(object sender, RoutedEventArgs e)
        {
            var cb = sender as CheckBox;
            cb.Content = Res.Rm.GetString("On");
            SetStatus(OptionsStatus.Modified);
        }

        private void BizssStateBox_OnUnchecked(object sender, RoutedEventArgs e)
        {
            var cb = sender as CheckBox;
            cb.Content = Res.Rm.GetString("Off");
            SetStatus(OptionsStatus.Modified);
        }

        bool CheckParam(string par, string value)
        {
            if (!par.Contains("err"))
            {
                if (value != par)
                    return true;
            }
            else
            {
                throw new Exception(par.Remove(0,4));
            }
            return false;
        }

        //True - нужен перезапуск
        bool CheckChanges()
        {
            try
            {
            //Logs delay
            if (CheckParam(DBwork.GetOptionValue(LogsDelayOptName), LogsTimeoutBox.Content.ToString()))
                return true;
            //var par = DBwork.GetOptionValue(LogsDelayOptName);
            //if (!par.Contains("err"))
            //{
            //    if (LogsTimeoutBox.Content.ToString() != par)
            //        return true;
            //}
            //else
            //{
                
            //}
            //Bizss state
            if (CheckParam(DBwork.GetOptionValue(BizssControlEnabledOptName), LogsTimeoutBox.Content.ToString()))
                return true;
            //par = DBwork.GetOptionValue(BizssControlEnabledOptName);
            //if (!par.Contains("err"))
            //{
            //    if (BizssStateBox.IsChecked.ToString() != par)
            //        return true;
            //}
            //else
            //{
            //Bizss delay
            if (CheckParam(DBwork.GetOptionValue(BizssControlDelayOptName), LogsTimeoutBox.Content.ToString()))
                return true;
            //par = DBwork.GetOptionValue(BizssControlDelayOptName);
            //if (!par.Contains("err"))
            //{
            //    if (BizssTimeoutBox.Content.ToString() != par)
            //        return true;
            //}
            //else
            //{

            //}
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return false;
            }
            return false;
        }

        bool OptionsSave()
        {
            bool toRet = CheckChanges();
            string ret = DBwork.AddOrSetOptionValue(LogsDelayOptName, LogsTimeoutBox.Value.ToString());
            if (ret.Contains("err"))
            {
                MessageBox.Show(ret.Remove(0, 4));
            }
            ret = DBwork.AddOrSetOptionValue(BizssControlEnabledOptName, BizssStateBox.IsChecked.ToString());
            if (ret.Contains("err"))
            {
                MessageBox.Show(ret.Remove(0, 4));
            }
            ret = DBwork.AddOrSetOptionValue(BizssControlDelayOptName, BizssTimeoutBox.Value.ToString());
            if (ret.Contains("err"))
            {
                MessageBox.Show(ret.Remove(0, 4));
            }
            return toRet;
        }

    }
}
